package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.PayProductTypeServiceApi;
import com.thb.pay.api.payment.vo.req.PayProductTypePageReq;
import com.thb.pay.api.payment.vo.req.PayProductTypeReq;
import com.thb.pay.api.payment.vo.resp.PayProductTypeVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayProductType;
import com.thb.pay.payment.service.PayProductTypeService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付产品类型表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PayProductTypeController implements PayProductTypeServiceApi {

    @Autowired
    private PayProductTypeService payProductTypeService;

    @Override
    public PageResult<PayProductTypeVo> findPage(@RequestBody PayProductTypePageReq pageReq) {
        IPage<PayProductType> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayProductType> pageResult = payProductTypeService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayProductTypeVo.class);
    }

    @Override
    public List<PayProductTypeVo> findAllList() {
        List<PayProductTypeVo> resultDataList = Lists.newArrayList();
        List<PayProductType> targetDbResultList = payProductTypeService.selectList(null);
        targetDbResultList.forEach(dbResult -> {
            PayProductTypeVo resultData = new PayProductTypeVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<PayProductTypeVo> findListByIds(@RequestBody List<Long> ids) {
        List<PayProductTypeVo> resultDataList = Lists.newArrayList();
        LambdaQueryWrapper<PayProductType> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayProductType::getId, ids);
        List<PayProductType> targetDbResultList = payProductTypeService.selectList(wrapper);
        targetDbResultList.forEach(dbResult -> {
            PayProductTypeVo resultData = new PayProductTypeVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public PayProductTypeVo getById(@RequestParam("id") Long id) {
        PayProductTypeVo payProductTypeVo = null;
        PayProductType payProductType = payProductTypeService.selectById(id);
        if (payProductType != null) {
            payProductTypeVo = new PayProductTypeVo();
            BeanUtil.copyProperties(payProductType, payProductTypeVo);
        }
        return payProductTypeVo;
    }

    @Override
    public void add(@RequestBody PayProductTypeReq payProductTypeReq) {
        LambdaQueryWrapper<PayProductType> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayProductType::getCode, payProductTypeReq.getCode());
        List<PayProductType> targetDbResultList = payProductTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            throw RRException.build("pay.product.type.exist");
        }
        PayProductType payProductType = new PayProductType();
        BeanUtil.copyProperties(payProductTypeReq, payProductType);
        payProductTypeService.insert(payProductType);
    }

    @Override
    public void update(@RequestBody PayProductTypeReq payProductTypeReq) {
        PayProductType payProductType = payProductTypeService.getById(payProductTypeReq.getId());
        if (payProductType == null) {
            throw RRException.build("pay.product.type.not.exist");
        }
        if (!payProductType.getCode().equals(payProductTypeReq.getCode())) {
            LambdaQueryWrapper<PayProductType> wrapper = new LambdaQueryWrapper();
            wrapper.eq(PayProductType::getCode, payProductTypeReq.getCode());
            List<PayProductType> targetDbResultList = payProductTypeService.selectList(wrapper);
            if (CollUtil.isNotEmpty(targetDbResultList)) {
                throw RRException.build("pay.product.type.exist");
            }
        }
        PayProductType targetPayProductType = new PayProductType();
        BeanUtil.copyProperties(payProductTypeReq, targetPayProductType);
        payProductTypeService.updateById(targetPayProductType);
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<PayProductType> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayProductType::getId, ids);
        List<PayProductType> targetDbResultList = payProductTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            payProductTypeService.updateBatchById(targetDbResultList);
        }
    }
}
